/************************************************************************/
/* huajiang.liu
/* c++ 大作业
/************************************************************************/
 
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;
static const int NUM = 5; 

static const string SolidNamesList[NUM] = { "dragon","ninja","iceman","lion","wolf" };
static const int SolidCostList[NUM] = {1,2,3,4,5};
static const int SolidAttractList[NUM] = {5,4,3,2,1};
class warrior {

 public:
  warrior(int id, int life_value, int attact, string pname, string pcolor):id_(id), life_value_(life_value), attact_(attact), pname_(pname), pcolor_(pcolor){};
  int id_, life_value_, attact_;
  string pname_;
  string pcolor_;
};

class HeadQuarter {
 public:
 HeadQuarter(string pcolor, int life_value, int cost_list[], int warrior_list[]):
 		pcolor_(pcolor), life_value_(life_value) {
	 n_ = 1;
	for (int i = 0; i < NUM; i++) {
		warrior_list_[i] = warrior_list[i];
		cout << "warrior_list_" << i << ":" << warrior_list_[i] << endl;
		warrior_life_cost_[i] = cost_list[i];
		warrior_names_[i] = SolidNamesList[i];
		warrior_attacks_[i] = SolidAttractList[warrior_list_[i]];
		warrior_num_list_[i] = 0;
		if (i == 0) {
			min_life_value_ = warrior_life_cost_[i];
		} else {
			min_life_value_ = std::min(min_life_value_, warrior_life_cost_[i]);
		}
	}
	warriors_.clear();
	total_warrior_num_ = 1000;
	make_warrior_success_ = false;
	stop_state_ = false;
	warrior_index_ = 0;
 };
 ~HeadQuarter() {};
 void Makewarrior(int time) {
	if (n_ > total_warrior_num_) {
		cout << setw(3) << setfill('0') << time << " " << pcolor_ << "headquarter stops making warriors" << endl;
		stop_state_ = true;
	}
	
	// cout << time << "->warrior_index->" << warrior_index << "warrior->" << warrior << "warrior_names->" << warrior_names_[warrior] << endl;
	make_warrior_success_ = false;
	while (!make_warrior_success_) {
		int warrior = warrior_list_[warrior_index_ % 5];
		if (warrior_life_cost_[warrior] <= life_value_) {
			// warriors_.emplace_back(warrior(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_));
			warrior_num_list_[warrior]++;
			cout << setw(3) << setfill('0') << time << " " << pcolor_ << " " << warrior_names_[warrior] << " "
			     << n_ << " born with strength " << warrior_life_cost_[warrior] << "," << warrior_num_list_[warrior]
				 << " in " << pcolor_ << "headquarter" << endl;
			++n_;
			life_value_ -= warrior_life_cost_[warrior];
			++warrior_index_;
			make_warrior_success_ = true;
		} else {
			++warrior_index_;
		}
		if (life_value_ < min_life_value_) {
			stop_state_ = true;
			cout << setw(3) << setfill('0') << time << " " << pcolor_ << " headquarter stops making warriors" << endl;
			break;
		}
	}


 };

 bool StopState() {
	 return stop_state_;
 }
 private:
 int life_value_, n_;
 string pcolor_;
 int warrior_list_[NUM]; // 小兵出生顺序 
 int total_warrior_num_;
 string warrior_names_[NUM];
 int warrior_life_cost_[NUM]; //
 int warrior_attacks_[NUM];
 vector<warrior> warriors_;
 int warrior_num_list_[NUM];
 bool make_warrior_success_;
 bool stop_state_;
 int warrior_index_;
 int min_life_value_;
};

int main() {
 int cost_list[NUM] = {0,1,2,3,4};
 int case_num = 0;
 int red_warrior_list[NUM] = {2,3,4,1,0}; // 小兵出生顺序 0,1,2,3,4分别代表"dragon","ninja","iceman","lion","wolf"
 int blue_warrior_list[NUM] = {3,0,1,2,4}; // 小兵出生顺序 0,1,2,3,4分别代表"dragon","ninja","iceman","lion","wolf"
 cin >> case_num;
 for (int i =0; i < case_num; i++) {
	 int life_value = 0;
	 cin >> life_value;
	 for (int j = 0;j < NUM; j++) {
		 cin >> cost_list[j];
	 }
	HeadQuarter red_headquarter("red", life_value, cost_list, red_warrior_list);
    HeadQuarter blue_headquarter("blue", life_value, cost_list, blue_warrior_list);
	cout << "case " << i << ":" <<  endl;
	int t = 0;
	while(1) {
		red_headquarter.Makewarrior(t);
		blue_headquarter.Makewarrior(t);
		if (red_headquarter.StopState() && blue_headquarter.StopState()) break;
	++t;
	}
	
 }

}